package top.atcra.rpc.serialization;

import top.atcra.rpc.config.ConfigLoader;
import top.atcra.rpc.serialization.impl.HessianSerializer;
import top.atcra.rpc.serialization.impl.JavaObjectSerializer;
import top.atcra.rpc.serialization.impl.JsonSerializer;

/**
 * 序列化器工厂
 *
 * @author atcra
 * @date 2025-03-26
 */
public class SerializerFactory {

    public static Serializer getSerializer(SerializerType serializeType) {
        switch (serializeType) {
            case SerializerType.jdk -> {
                return new JavaObjectSerializer();
            }
            case SerializerType.json -> {
                return new JsonSerializer();
            }
            case SerializerType.hessian -> {
                return new HessianSerializer();
            }
            case null, default -> throw new RuntimeException("不支持的序列化器类型:" + serializeType);
        }
    }

    public static Serializer getSerializer(byte serializeId) {
        var type = SerializerType.valueOf(serializeId);
        return getSerializer(type);
    }

    public static Serializer getSerializer() {
        var serializeType = SerializerType.valueOf(ConfigLoader.getConfig().getSerializer());
        return getSerializer(serializeType);
    }
}
